
import EventEmitter from 'events'
export class SelectionModel extends EventEmitter{
  private textModel_;
  private selection_;
  constructor(textModel: any) {
    super()
    this.textModel_ = textModel
    this.selection_ = {anchor: 0, focus: 0}
  }
  /**
   * 
   * @returns 返回当前的选择内容
   */
  getSelection() {
    return { ...this.selection_};
  }

  /**
   * 
   * @param selection 设置选择的内容
   */
  setSelection(selection: { anchor: number; focus: number; }) {
    if (selection && this.selection_ && selection.anchor === this.selection_.anchor && selection.focus === this.selection_.focus) {
      // 说明光标在最后 不需要重新设置光标
      return 
    }
     selection && (this.selection_ = {...selection}) 
  }

  /**
   * 
   * @returns 判断选择是否重叠
   */
  isOverlap() {
    return this.selection_ && this.selection_.anchor === this.selection_.focus
  }

  /**
   * @description 判断是否是从后向前选择
   */
  isBackward(){
    return this.selection_ && this.selection_.anchor > this.selection_.focus
  }
}

export default SelectionModel